﻿using System.Linq;
using Blog.Core.Service;
using Nancy;
using Newtonsoft.Json;

namespace Web.Module
{
    public class HomeModule : BaseModule
    {
        private readonly PostService _postService = new PostService();
        public HomeModule()
        {
            //首页
            Get["/"] = _ => ReturnHomeAction(1,0);

            //首页分页
            Get["/page/{page:int}"] = p => ReturnHomeAction(p.page, 0);         

            //关于
            Get["/about"] = p => View["about"];

            //文章
            Get["/post/{id:int}"] = p => ReturnPostAction(p.id);

            Get["/category/life"] = p => View["category"];

            Get["/category/code"] = p => View["category"];
        }

        public dynamic ReturnHomeAction(int page, int categoryId)
        {
            var model = _postService.GetPostList(page, 5, categoryId);
            return View["index", model];            
        }
        public dynamic ReturnPostAction(int id)
        {
            var model = _postService.GetPostInfo(id);
            return View["post", model];
        }
    }
}